//
//  IslanInforChangeCtl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/5.
//  Copyright © 2016年 game. All rights reserved.
#import "IslanInforChangeCtl.h"
#import "SelectNavView.h"
#import "ISlInformChangeCell.h"
#import "DCLeixingCtrl.h"
#import "SFLeiXingCtrl.h"
#import "SMyuController.h"
#import "InfmTabHeadClickView.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "VPImageCropperViewController.h"
#import "IslanInformChangeModel.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreLocation/CoreLocation.h>
#import "ChangeLocalCtrl.h"
#import "PersonEditModel.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFURLSessionManager.h"

@interface IslanInforChangeCtl ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,CellClickDeleget,UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, VPImageCropperDelegate>
{
    NSString *_latitude;
    NSString *_longitute;
    NSString *_cityPinY;
    NSString *_cityCode;
    NSString *_location;
    
    NSString *_charge_money;
}
@property (nonatomic,strong) SelectNavView *naV;
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) InfmTabHeadClickView *headClick;



/**
 *  tabView的头部
 */
@property (nonatomic,strong) UIButton *tabHeard;
/**
 *  记录键盘弹起前tableview的偏移
 */
@property (nonatomic,assign) CGPoint fronOffset;
@property (nonatomic,strong) IslanInformChangeModel *Model;
@property (nonatomic,copy) NSString *charge_type;
@property (nonatomic,copy) NSString *charge_sum;
@property (nonatomic,copy) NSString *typeIdString;
@property (nonatomic,copy) NSString *fishIdString;
@property (nonatomic,strong) NSData *imageData;
@property (nonatomic,strong) MBProgressHUD *Myhud;

@property (nonatomic,strong) NSDictionary *dataDic;
@property (nonatomic,assign) BOOL isEdit;

@property (nonatomic,strong) NSMutableArray *filedImgAry;
@end

@implementation IslanInforChangeCtl

-(NSMutableArray *)filedImgAry{
    if (!_filedImgAry) {
        _filedImgAry = [NSMutableArray array];
    }

    return _filedImgAry;
}

-(MBProgressHUD *)Myhud{
    
    if (!_Myhud) {
        _Myhud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        _Myhud.removeFromSuperViewOnHide = YES;
        _Myhud.labelText = @"正在上传...";
        _Myhud.labelColor=[UIColor whiteColor];
        _Myhud.color=[UIColor blackColor];
        _Myhud.activityIndicatorColor = [UIColor whiteColor];//菊花颜色
        _Myhud.removeFromSuperViewOnHide = YES;
        [_Myhud show:YES];
        [self.view addSubview:_Myhud];
    }
   
    return _Myhud;
}

-(UIButton *)tabHeard{
    if (!_tabHeard) {
        _tabHeard = [UIButton buttonWithType:UIButtonTypeCustom];
        _tabHeard.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/2);
        _tabHeard.backgroundColor = [UIColor redColor];
        [_tabHeard addTarget:self action:@selector(TabHeadClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _tabHeard;
}

-(void)TabHeadClick{
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    self.headClick.alpha = 1;
}

-(InfmTabHeadClickView *)headClick{
    if (!_headClick) {
        _headClick = [[NSBundle mainBundle]loadNibNamed:@"InfmTabHeadClickView" owner:nil options:nil].lastObject;
        _headClick.alpha = 0;
        _headClick.frame = self.view.frame;
        [_headClick.paizhaobtn addTarget:self action:@selector(paizhaoClick) forControlEvents:UIControlEventTouchUpInside];
        [_headClick.xiangcebtn addTarget:self action:@selector(xiangClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_headClick];
    }
        return _headClick;
}
// 拍照
-(void)paizhaoClick{
    
    if ([Manager isCameraAvailable] && [Manager doesCameraSupportTakingPhotos]) {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.sourceType = UIImagePickerControllerSourceTypeCamera;
        NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
        [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
        controller.mediaTypes = mediaTypes;
        controller.delegate = self;
        [self presentViewController:controller animated:YES completion:nil];
    }

        self.headClick.alpha = 0;
}

//打开相册点击
-(void)xiangClick{
    
    if ([Manager isPhotoLibraryAvailable]) {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
        [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
        controller.mediaTypes = mediaTypes;
        controller.delegate = self;
        [self presentViewController:controller animated:YES completion:nil];
    }
        self.headClick.alpha = 0;
}

#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {//选取点击
    
    UIImage *image = [Manager imageCompressForWidth:editedImage targetWidth:640];
    self.imageData = UIImageJPEGRepresentation(image,0.8);
    
    [self.imageData writeToFile:@"/Users/diaoyuren/Desktop/钓场图片.jpeg" atomically:YES];

    [self.tabHeard setBackgroundImage:editedImage forState:UIControlStateNormal];
    [cropperViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {//取消
    [cropperViewController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 2.UIImagePickerController的委托
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^() {
        
        UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        // 设置裁剪
        VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:[Manager fixOrientation:portraitImg] cropFrame:CGRectMake(0, self.view.frame.size.height*(1-0.625), self.view.frame.size.width, self.view.frame.size.width*0.625) limitScaleRatio:2];
        imgEditorVC.delegate = self;
        [self presentViewController:imgEditorVC animated:YES completion:nil];
    }];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        [_tableview registerClass:[ISlInformChangeCell class] forCellReuseIdentifier:@"cell"];
       
        _tableview.backgroundColor = [UIColor clearColor];
    }
    return _tableview;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.app.myTabBar.tabBar.hidden = YES;
    [self CreatNavView];
}

-(void)CreatNavView{
    self.naV = [[SelectNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    
    self.naV.titile.text = @"编辑钓场";
    
    if (self.isYuJuDian) {
        self.naV.titile.text = @"编辑渔具店";
    }
    self.naV.share.titleLabel.font = font(SCREEN_WIDTH*0.05);
    [self.naV.share setTitle:@"提交" forState:UIControlStateNormal];
    self.naV.share.y = 21;
    self.naV.share.x = SCREEN_WIDTH - 60;
    [self.view addSubview:self.naV];
    [self.naV.bacBtn addTarget:self action:@selector(Back) forControlEvents:UIControlEventTouchUpInside];
    [self.naV.share addTarget:self action:@selector(Tijiao) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark--点击返回判断是否要保存草稿
-(void)IsGetEdit{

    self.isEdit = YES;
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    ISlInformChangeCell *cell = [self.tableview cellForRowAtIndexPath:indexPath];
    cell.tijiao = @"YES";
    
    IslanInformChangeModel *model = [[IslanInformChangeModel alloc]initWithDictionary:self.dataDic error:nil];
    
    if (model.name.length!=0||model.desc.length!=0||self.imageData!=nil) {
        
        
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:@"是否保存草稿?" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *ToPhone = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self Edit];
        }];
        
        UIAlertAction *ToImage = [UIAlertAction actionWithTitle:@"不保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self Goback];
        }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [aler addAction:ToPhone];
        [aler addAction:ToImage];
        [aler addAction:cancel];
        [self presentViewController:aler animated:YES completion:nil];
        
        
        
    }else{
        
        [self Goback];
    }

}

-(void)Edit{

    IslanInformChangeModel *model = [[IslanInformChangeModel alloc]initWithDictionary:self.dataDic error:nil];
    NSString *name = @"新钓场[钓场]";
    if (self.isYuJuDian) {
        name = @"新渔具店[渔具店]";
    }
    
    if (model.name.length!=0) {
        name = [NSString stringWithFormat:@"%@[钓场]",model.name];
        if (self.isYuJuDian) {
            name =[NSString stringWithFormat:@"%@[渔具店]",model.name];
        }
    }
    NSString *text;
    
    if (model.desc.length!=0) {
        text = model.desc;
    }else{
        text = @"";
    }
    
 
    NSDictionary *dic = @{
                          @"time":[Manager AchiveNowTime],
                          @"images":@[self.imageData],
                          @"name":name,
                          @"type":@"DiaochangPolad",
                          @"text":text,
                          @"Id":@"",
                          @"DCDic":self.dataDic,
                           @"isyujud":[NSString stringWithFormat:@"%d",self.isYuJuDian],
                          };
    
    self.filedImgAry = [NSMutableArray arrayWithContentsOfFile:[Manager editeFilePath]];
    
    [self.filedImgAry insertObject:dic atIndex:0];
    
    [self.filedImgAry writeToFile:[Manager editeFilePath] atomically:YES];
    
    [self.filedImgAry removeAllObjects];
    if ([self.deleget respondsToSelector:@selector(GetToEdit)]) {
        [self.deleget GetToEdit];
    }
    [self Goback];


}

#pragma mark--点击返回
-(void)Back{
    if (self.number==30||self.number==40) {
        
        [self IsGetEdit];
        
    }else{
    
        [self Goback];
    
    }
    
}

-(void)Goback{
    
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {//push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{//present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
#pragma mark--点击提交 通过cell.tijiao 点Set方法 遍历tableview的cell上的textField 再用过代理传回Textfile上的值
-(void)Tijiao{
    
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    ISlInformChangeCell *cell = [self.tableview cellForRowAtIndexPath:indexPath];
    cell.tijiao = @"YES";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableview];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = RGBA(230, 230, 238, 1);
    
    if (self.number==30||self.number==40) {
        self.tableview.tableHeaderView = self.tabHeard;
    }
}

-(void)setModule:(NSString *)module{
    _module = module;
    
    [[Manager sharedManager]ResponseWhithURL:[NSString stringWithFormat:islanChange,_module] AndParameter:@{@"d_id":self.ID} AndCompletionType:^(id responseObject, DIEError *error) {
        
        if ([responseObject[@"status"]isEqualToString:@"SUCCESS"]) {
  
            self.Model = [[IslanInformChangeModel alloc]initWithDictionary:responseObject[@"data"][@"detail"] error:nil];
            [self.tableview reloadData];
            
            self.charge_type = self.Model.charge_type;
            self.charge_sum = self.Model.charge_sum;
            self.typeIdString = self.Model.type;
            self.fishIdString = self.Model.fish;
        }
        
    }];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
        return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    tableView.separatorStyle = 0;

    ISlInformChangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  
    cell.isYuJuDian = self.isYuJuDian;
    cell.deleget = self;
    cell.model = self.Model;
    
    cell.number = self.number;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT-64;
}

#pragma mark---键盘落下
-(void)keboardDown{
        self.tableview.contentOffset = CGPointMake(0, H(_tabHeard)/2);
}
#pragma mark---键盘升起
-(void)keboardUpWhithView:(UIView *)view AndKeboardRect:(CGRect)keboardrect{
    
   CGPoint point = [view convertPoint:CGPointZero toView:self.view];
    CGPoint atTabPoint = [view convertPoint:CGPointZero toView:self.tableview];
    
    CGFloat yyy = atTabPoint.y;
    if (view.tag==1618) {
        UITextView *textview = (UITextView *)view;
        yyy = atTabPoint.y + textview.contentOffset.y;
    }
   
    if (((point.y+H(view)))>keboardrect.origin.y) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [UIView animateWithDuration:0.3 animations:^{
                self.tableview.contentOffset = CGPointMake(0, (yyy+15)-(SCREEN_HEIGHT-64-keboardrect.size.height-H(view)));
                
            
            }];
        });
        
    }
}

#pragma mark--ISlInformChangeCellDeleget
#pragma mark--点击钓场类型
-(void)DiaochangLeiXing:(UITextField *)textfield{
    DCLeixingCtrl *Leixing = [DCLeixingCtrl new];
    
    Leixing.sssst = textfield.text;
    Leixing.DCLeixingBlock = ^(NSString *ssst,NSString *idString){
        
        textfield.text = ssst;
        self.typeIdString = idString;
    };
    if (self.number==30||self.number==40) {
        [self presentViewController:Leixing animated:YES completion:nil];
    }else{
        [self.navigationController pushViewController:Leixing animated:YES];
    }
}

#pragma mark--点击收费类型
-(void)ShoufeiLeixing:(UITextField *)textfield{
    
    SFLeiXingCtrl *sf = [SFLeiXingCtrl new];
    sf.selectResult = textfield.text;
    
    if (self.number==30||self.number==40) {
        [self presentViewController:sf animated:YES completion:nil];
    }else{
        [self.navigationController pushViewController:sf animated:YES];
    }

    sf.SFBlock = ^(NSString *string,NSString *charge_type,NSString *charge_sum){
        
        textfield.text = string;
        _charge_money = string;
        self.charge_type = charge_type;
        self.charge_sum = charge_sum;
    };
} 
#pragma mark--点击有什么鱼
-(void)YouSmYu:(UITextField *)textfield{
    
    SMyuController *smyu = [SMyuController new];
    
    if (self.number==30||self.number==40) {
        [self presentViewController:smyu animated:YES completion:nil];
    }else{
        [self.navigationController pushViewController:smyu animated:YES];
    }
    
    smyu.rerult = textfield.text;
    
  
    smyu.SMyuBlock = ^(NSString *result,NSString *idString){
    
        textfield.text = result;
        self.fishIdString = idString;
    };
}

//发布时  还没有输入钓场位置  点击钓场位置这个cell时 跳转到地图修改位置控制器
-(void)NoLocalMesssageWhithTextField:(UITextField *)textField{
[[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    
    if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {//没有打开定位服务
        NSString *string = @"建议您打开定位，提高您与钓场距离的准确性";
        if (self.isYuJuDian) {
           string = @"建议您打开定位，提高您与渔具店距离的准确性";
        }
        UIAlertController *alert = [self ShowAlertWhthTitle:@"打开定位" Message:string CancelButtonTitle:@"取消"];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
            NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if([[UIApplication sharedApplication] canOpenURL:url]) {
                NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];
                [[UIApplication sharedApplication] openURL:url];
            }
            
        }];
        
        [alert addAction:cancel];
        
    }else{//打开了
        
        ChangeLocalCtrl *change = [ChangeLocalCtrl new];
        change.ChangeBlobk = ^(NSString *localname,NSString *latitude,NSString *longitute,NSString *cityPinY,NSString *cityCode){
            
            textField.text = localname;
            _latitude = latitude;
            _longitute = localname;
            _cityCode = cityCode;
            _cityPinY = cityPinY;
            _location = localname;
        
        };
        [self presentViewController:change animated:YES completion:nil];
    }
}
#pragma mark---点击提交 通过点Set方法 遍历tableviewCell上的内容 通过代理传回Dic
-(void)TijiaoWhithDic:(NSDictionary *)dic{

    NSMutableDictionary *tijiaoDic = [NSMutableDictionary dictionary];
    [tijiaoDic addEntriesFromDictionary:dic];

    [tijiaoDic setValue:self.charge_type forKey:@"charge_type"];
    [tijiaoDic setValue:self.charge_sum forKey:@"charge_sum"];
    [tijiaoDic setValue:self.typeIdString forKey:@"type"];
    [tijiaoDic setValue:self.fishIdString forKey:@"fish"];
    [tijiaoDic setValue:_cityCode forKey:@"city_code"];
    [tijiaoDic setValue:_cityCode forKey:@"city_code"];
    [tijiaoDic setValue:_longitute forKey:@"longitude"];
    [tijiaoDic setValue:_latitude forKey:@"latitude"];
    
    if (self.isEdit) {//是否是用于点击返回的判断
        [tijiaoDic setValue:_charge_money forKey:@"charge_money"];
        [tijiaoDic setValue:_location forKey:@"location"];
        self.dataDic = tijiaoDic;
        self.isEdit = NO;
    }else{
    
        
        IslanInformChangeModel *model = [[IslanInformChangeModel alloc]initWithDictionary:tijiaoDic error:nil];
        
        if (self.number==30||self.number==40) {//发布提交
            
            [tijiaoDic setValue:_location forKey:@"location"];
            [self NewIslanPoladDic:tijiaoDic];
            
        }else{//修改提交
            
            if (_location.length!=0) {
                [tijiaoDic setValue:_location forKey:@"location"];
            }
            
            if (model.name.length==0) {
                [Manager GetHudWhithText:@"名称不能为空" AndFatherView:self.view];
                
            }else if (model.location.length==0) {
                [Manager GetHudWhithText:@"地理位置不能为空" AndFatherView:self.view];
                
                
            }else if (model.identity.length==0) {
                [Manager GetHudWhithText:@"请选择您的身份" AndFatherView:self.view];
                
            }else{
                
                if (!self.isYuJuDian) {
                    
                    if (model.type.length==0) {
                        [Manager GetHudWhithText:@"请选择钓场类型" AndFatherView:self.view];
                        
                    }else  if (model.charge_type.length==0) {
                        [Manager GetHudWhithText:@"请请选择收费类型" AndFatherView:self.view];
                    }else{
                        
                        [self PostDataWhitDic:tijiaoDic];
                    }
                }else{
                    
                    [self PostDataWhitDic:tijiaoDic];
                }
            }
            
        }

    
    }
}

#pragma mark--上传错误
-(void)PostDataWhitDic:(NSMutableDictionary *)dic{
    
     [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            
            [dic addEntriesFromDictionary:@{@"formhash":responseObject[@"data"][@"formhash"],@"d_id":self.ID}];
        
            [[Manager sharedManager]ResponseWhithURL:[NSString stringWithFormat:islanChange,self.module] AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
                
                if (!error) {
                    if ([responseObject[@"status"]isEqualToString:@"SUCCESS"]) {
                        
                        [Manager GetHudWhithText:@"提交成功" AndFatherView:self.view];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [self.navigationController popViewControllerAnimated:YES];
                        });
                    }else{
                    
                        [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
                    }

                }else{
            
                    [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
                }
            }];
        }
    }];
}

#pragma mark---提交新钓场或渔具店
-(void)NewIslanPoladDic:(NSMutableDictionary *)dic{

    IslanInformChangeModel *model = [[IslanInformChangeModel alloc]initWithDictionary:dic error:nil];
    
    if (!self.imageData) {
        [Manager GetHudWhithText:@"请添加图片" AndFatherView:self.view];
        
    }else if (model.name.length==0){
        [Manager GetHudWhithText:@"名称不能为空" AndFatherView:self.view];
        
    } else if (model.location.length==0) {
        [Manager GetHudWhithText:@"请选择位置" AndFatherView:self.view];
        
    }else if (model.identity.length==0) {
        [Manager GetHudWhithText:@"请选择您的身份" AndFatherView:self.view];
        
    }else if (model.contacts.length==0) {
        [Manager GetHudWhithText:@"请输入联系人姓名" AndFatherView:self.view];
        
    }else if (model.phone.length==0) {
        
        [Manager GetHudWhithText:@"请输入联系电话" AndFatherView:self.view];
    }else if (model.desc.length==0) {
        
        [Manager GetHudWhithText:@"请输入描述" AndFatherView:self.view];
    }else{
        
        if (!self.isYuJuDian) {
            
            if (model.type.length==0) {
                [Manager GetHudWhithText:@"请选择钓场类型" AndFatherView:self.view];
                
            }else  if (model.charge_type.length==0) {
                [Manager GetHudWhithText:@"请请选择收费类型" AndFatherView:self.view];
            }else if (model.fish.length==0){
                
                [Manager GetHudWhithText:@"请请选择鱼的种类" AndFatherView:self.view];
            }else{
                
                [self.Myhud show:YES];
                [self NewislanGetFormhashWhithDic:dic];
            }
            
        }else{
            
            [self.Myhud show:YES];
            [self NewislanGetFormhashWhithDic:dic];

        }
    }

}

-(void)NewislanGetFormhashWhithDic:(NSMutableDictionary *)dic{

     [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
         if (error) {
             [self Edit];
             [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
         }else

        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {

        [dic addEntriesFromDictionary:@{@"formhash":responseObject[@"data"][@"formhash"]}];
            [self poladImageWhithFormhash:responseObject[@"data"][@"formhash"] AndDic:dic];
        }
    }];

}
-(void)poladImageWhithFormhash:(NSString *)formHash AndDic:(NSMutableDictionary *)dic{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSString *url = islanUploadImage;
    if (self.isYuJuDian) {
        url = dianpuUploadImage;
    }
    
     [manager POST:url parameters:@{@"formhash":formHash,@"id":@"0",@"type":@"1"} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:self.imageData name:@"Filedata.jpg" fileName:@"DC" mimeType:@"application/octet-stream"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            
            [dic addEntriesFromDictionary:@{@"pic_ids":responseObject[@"data"][@"dp_id"]}];
            [self PostOneNewWhithDic:dic];
            
        }else{
            
            [self Edit];
            [MBProgressHUD hideHUDForView:self.view];
            [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [self Edit];
        [MBProgressHUD hideHUDForView:self.view];
        [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
        
    }];
    
}


-(void)PostOneNewWhithDic:(NSMutableDictionary *)dic{
    NSString *model = @"diaochang";
    if (self.number==40) {
        model = @"dianpu";
    }
    
    [[Manager sharedManager]ResponseWhithURL:[NSString stringWithFormat:GetNewIslanislan,model] AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (!error) {
            
            if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
                
                [MBProgressHUD hideHUDForView:self.view];
                [Manager GetHudWhithText:@"添加成功" AndFatherView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self dismissViewControllerAnimated:YES completion:nil];
                });
                
            }else{
                
                [self Edit];
                [MBProgressHUD hideHUDForView:self.view];
                [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
            }

        }else{
            [self Edit];
            [MBProgressHUD hideHUDForView:self.view];
            [Manager GetHudWhithText:@"提交失败" AndFatherView:self.view];
        }
    }];
}

-(void)setEditDic:(NSDictionary *)editDic{
    _editDic = editDic;
    
    PersonEditModel *model = [[PersonEditModel alloc]initWithDictionary:_editDic error:nil];
    if (model.images.count!=0) {
        self.imageData = model.images.firstObject;
        [self.tabHeard setImage:[UIImage imageWithData:self.imageData] forState:UIControlStateNormal];
    }
    
    self.Model = [[IslanInformChangeModel alloc]initWithDictionary:model.DCDic error:nil];
    [self.tableview reloadData];
    
    self.charge_type = self.Model.charge_type;
    self.charge_sum = self.Model.charge_sum;
    self.typeIdString = self.Model.type;
    self.fishIdString = self.Model.fish;
}

-(void)setIsAnew:(BOOL)isAnew{
    _isAnew = isAnew;

    [self Tijiao];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
[[[UIApplication sharedApplication]keyWindow]endEditing:YES];

}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}
@end